package prjtgl.util;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Classe permettant d'envoyer un email
 *
 * @author Amirouche
 */
public class Email {

	public static void envoyer(String destinataire, String sujet, String contenu) {

		// Nom d'utilisateur de la boite d'envoi
		final String username = "res.soc.pps@gmail.com";
		
		// Mot de passe de la boite d'envoi
		final String password = "1551XG27";

		// Connexion au serveur Google
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		// Authentification sur le serveur
		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});

		try {

			// Creation du message
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("mysite@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(destinataire));
			message.setSubject(sujet);
			message.setText(contenu);

			// Envoi
			Transport.send(message);

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}

	}
}
